/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.splitor;

import cn.ac.iie.di.global.GlobalParas;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import cn.ac.iie.di.tools.FileTools;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import org.apache.commons.io.FileUtils;

/**
 *
 * @author YG
 */
public class FileListener implements Runnable {

    public static Logger logger = Logger.getLogger(FileListener.class.getName());
    String dir;
    ArrayBlockingQueue dataQueue = null;

    public FileListener(String filePath, ArrayBlockingQueue dataQueue) {
        this.dir = filePath;
        this.dataQueue = dataQueue;
    }

    @Override
    public void run() {

        while (true) {
            try {
                if(GlobalParas.FILE_LISTENER){
                    break;
                }
                List<File> sigFiles = FileTools.getFiles(new File(dir), GlobalParas.FILENAME_SUFFIX);

                for (File sigFile : sigFiles) {
                    String dataFilePath = sigFile.getPath();
                    File filelock = new File(sigFile.getPath() + ".lock");
                    if (!filelock.exists()) {
                        FileTools.creatTxtFile(dataFilePath + ".lock");
                        dataQueue.put(dataFilePath);
//                        while (!dataQueue.offer(dataFilePath)) {
//                            logger.warn("Put Json FILE_NAME_QUEUE Fail!");
//                            Thread.sleep(3000);
//                        }   
                    }
                }
                Thread.sleep(1000);
            } catch (Exception e) {
                logger.error("FileListener error is : " + e, e);
            }
        }

    }

    public static void main(String[] args) {
        FileListener fl = new FileListener("D:\\123\\111", new ArrayBlockingQueue(10));
        Thread t = new Thread(fl);
        t.start();
    }
}
